#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author:    御风
# @Mail:      zhong.yufeng@foxmail.com
# @Time:      2022/4/12 0:37


import config
from flask import current_app as app
from flask_cors import cross_origin
from sqlalchemy.exc import InternalError
from modules.area import AreaDataBase
from utils.result import ApiResult
from .goods import route


# http://127.0.0.1:9001/goods/area
@route.route(r"/area", methods=["GET"], strict_slashes=False)
@cross_origin()
def apiArea():
    """
    配送地区
    """
    try:
        try:
            data = app.cache.get("area")
        except Exception:
            data = None
        if data is None:
            db = AreaDataBase()
            data = db.areasDetails
            if data:
                app.cache.set("area", data, timeout=config.CACHE_TIMEOUT)
    except ValueError as e:
        return ApiResult.error(201001, msg=str(e))
    except InternalError as e:
        app.logger.error(f"[数据库] {e}")
        return ApiResult.error(201002, msg="失败")
    except Exception as e:
        app.logger.error(f"[错误] {e}")
        return ApiResult.error(201003, msg="失败")
    return ApiResult.success(data, msg="成功")
